<script>

$(document).ready(function() {
	var address;
	$('#states').trigger("change");
});

	$('#zipcode').live('blur', function(){
			url = "/address/get-address?cep="+$(this).val(),
			
			$.get(url, function(data){
				address = data;
				if(data != null){
					$('#addre').val(address.address_type+" "+address.address);
	
					$('#neigh').val(address.neighborhood);

					if (address != null) {
						if(address != null && address.address != undefined){
							$('#citye option[value="' + address.city + '"]').attr({
								selected : "selected"
							});
						}else{
							$('#citye option[value="'+$('#cityC').val()+'"]').attr({
								selected : "selected"
							});
						}
					}
					
					if (address != null) {
						$('#states option[value="' + address.state + '"]').attr({
							selected : "selected"
						});
					} else {
						$('#states option[value=""]').attr({
							selected : "selected"
						});
					}
				}else{
					$('#addre').val("");
					
					$('#neigh').val("");
	
					$('#citye').val("");
					
					$('#states option[value=""]').attr({
						selected : "selected"
					});
				
					alert('CEP não encontrado!');
				}
			}, 'json')
	})

	$('.type').live('click', function(){
		$('.type').removeClass('checked');
		$('.type').addClass('unchecked');
		$(this).removeClass('unchecked');
		$(this).addClass('checked');
		$('#typeEdit').val($(this).prev().val());
	})
	
	$('.sex').live('click', function(){
		$('.sex').removeClass('checked');
		$('.sex').addClass('unchecked');
		$(this).removeClass('unchecked');
		$(this).addClass('checked');
		$('#sexEdit').val($(this).prev().val());
	})
	
    $('#send-login').live('click',function() {
		$.post($('#changeLogin').attr("action"), $('#changeLogin').serialize(),function (data) {
			$('#login-form').html(data);
		});
	});

	$('#send-client').live('click',function() {
		$.post($('#editClient').attr("action"), $('#editClient').serialize(),function (data) {
			$('#client-form').html(data);
		});
	});

	$('#send-address').live('click',function() {
		$.post($('#editAddress').attr("action"), $('#editAddress').serialize(),function (data) {
			$('#address-form').html(data);
		});
	});

	$('#states').live('change', function(){
		url='/address/get-cities?state='+$(this).val();

		$.get(url, function(data){
			$('#citye').html(data);

			if(address.city != null && address.city != undefined){
				$('#citye option[value="' + address.city + '"]').attr({
					selected : "selected"
				});
			}else{
				$('#citye option[value="'+$('#cityE').val()+'"]').attr({
					selected : "selected"
				});
			}
		})
	});

</script>

<div id="list-products">
	<div style="display: inline-block;" class="register">
		<div class="block">
			<div class="top">Dados do Cadastro</div>
			<div id="login-form">
				<form id="changeLogin" action="/client/edit-login">	
					<br />
					<span>Login: </span>
					<input type="text" class="text" name="login" id="login" size="15" maxlength="15" <?php if(isset($this->errorMessages['login'])):?>placeholder="<?php echo $this->errorMessages['login']?>" <?php else :?>value="<?php echo $this->client['username']?>" <?php endif;?>>
					<br /><br />
					<span>E-mail: </span>
					<input type="text" class="text" name="email" id="email" size="15" maxlength="65" <?php if(isset($this->errorMessages['email'])):?>placeholder="<?php echo $this->errorMessages['email']?>" <?php else :?>value="<?php echo $this->client['email']?>" <?php endif;?>>
					<br /><br />
					<input id="send-login" type="button" value="Editar" style="float: right; margin-right: 10px;">
					<br />
				</form>
			</div>
		</div>
		
		<div class="block">
			<div class="top">Dados Pessoais</div>
			<div id="client-form">
				<form id="editClient" action="/client/edit-client">
					<br />
					<span>Nome: </span>
					<input type="text" class="text" name="name" id="name" size="15" maxlength="65" <?php if(isset($this->errorMessages['name'])):?>placeholder="<?php echo $this->errorMessages['name']?>" <?php else :?>value="<?php echo $this->client['name']?>" <?php endif;?>>
					<br /><br />
					<span>Cpf: </span>
					<input onkeyup="maskCpf(this);" type="text" class="text" name="cpf" id="cpf" size="15" maxlength="15" <?php if(isset($this->errorMessages['cpf'])):?>placeholder="<?php echo $this->errorMessages['cpf']?>" <?php else :?>value="<?php echo $this->client['cpf']?>" <?php endif;?>>
					
					<br /><br />
					<span>Sexo: </span>
					<input type="hidden" id="sexEdit" name="sex" value="<?php echo $this->client['sex']?>">
					<input id="fem" value="Feminino" type="radio">
					<label id="fe" for="fem" class="sex <?php if($this->client['sex'] == "Feminino"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">Feminino</label>
					
					<input id="man" value="Masculino" type="radio">
					<label id="ma" for="man" class="sex <?php if($this->client['sex'] == "Masculino"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">Masculino</label>
					
					<?php if(isset($this->errorMessages['sex'])):?>
						<label style="font-size: 20px; margin-left: 20px;"><?php echo $this->errorMessages['sex']?></label>
					<?php endif;?>
					<br /><br />
					<span>Data de nascimento: </span>
					<input onkeyup="maskDate(this);" type="text" class="text" name="birth" id="birth" size="15" maxlength="15" <?php if(isset($this->errorMessages['birth'])):?>placeholder="<?php echo $this->errorMessages['birth']?>" <?php else :?>value="<?php echo $this->client['birth']?>" <?php endif;?>>
					<br /><br />
					<span>Telefone Preferencial: </span>
					<input onkeyup="maskPhone(this);" type="text" class="text" name="phone" id="phone" size="15" maxlength="15" <?php if(isset($this->errorMessages['phone'])):?>placeholder="<?php echo $this->errorMessages['phone']?>" <?php else :?>value="<?php echo $this->client['phone']?>" <?php endif;?>>
					<br /><br />
					<span>Celular: </span>
					<input onkeyup="maskPhone(this);" type="text" class="text" name="cel" id="cel" size="15" maxlength="15" <?php if(isset($this->errorMessages['celphone'])):?>placeholder="<?php echo $this->errorMessages['celphone']?>" <?php else :?>value="<?php echo $this->client['celphone']?>" <?php endif;?>>
					<br /><br />
					<br /><br />
					<input id="send-client" type="button" value="Editar" style="float: right; margin-right: 10px;">
					<br />
				</form>
			</div>
		</div>
		
		<div class="block">
			<div class="top">Dados de Envio</div>
			<div id="address-form">
				<form id="editAddress" action="/client/edit-address">
					<br />
					<span>Cep: </span>
					<input onkeyup="maskCep(this);" type="text" class="text" name="zipcode" id="zipcode" size="15" maxlength="13" <?php if(isset($this->errorMessages['zipcode'])):?>placeholder="<?php echo $this->errorMessages['zipcode']?>" <?php else :?>value="<?php echo $this->client['zipcode']?>" <?php endif;?>>
					<br /><br />
					<span>Endereço: </span>
					<input type="text" class="text" name="address" id="addre" size="15" maxlength="65" <?php if(isset($this->errorMessages['address'])):?>placeholder="<?php echo $this->errorMessages['address']?>" <?php else :?>value="<?php echo $this->client['address']?>" <?php endif;?>>
					<br /><br />
					<span>Bairro: </span>
					<input type="text" class="text" name="neighborhood" id="neigh" size="15" maxlength="65" <?php if(isset($this->errorMessages['neighborhood'])):?>placeholder="<?php echo $this->errorMessages['neighborhood']?>" <?php else :?>value="<?php echo $this->client['neighborhood']?>" <?php endif;?>>
					<br /><br />
					<span>Número: </span>
					<input type="text" class="text" name="number" id="number" size="15" maxlength="6" <?php if(isset($this->errorMessages['number'])):?>placeholder="<?php echo $this->errorMessages['number']?>" <?php else :?>value="<?php echo $this->client['number']?>" <?php endif;?>>
					<br /><br />
					<div >
					<span>Tipo: </span>
						<input type="hidden" id="typeEdit" name="type" value="<?php echo $this->client['type']?>">
						<input id="ape" value="Apartamento" class="card-buttom" name="ship" class="formLeft" type="radio" >
						<label id="ap" for="ape" class="type <?php if($this->client['type'] == "Apartamento"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">Apartamento</label>
						<input id="cas" value="Casa" class="card-buttom" name="ship" class="formLeft" type="radio" >
						<label id="ca" for="cas" class="type <?php if($this->client['type'] == "Casa"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">Casa</label>
						<input id="com" value="Comercial" class="card-buttom" name="ship" class="formLeft" type="radio" >
						<label id="co" for="com" class="type <?php if($this->client['type'] == "Comercial"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">Comercial</label>
					</div>
					<?php if(isset($this->errorMessages['type'])):?>
						<div style="float: left; margin-top: 25px;">
							<label style="font-size: 20px; float: left;"><?php echo $this->errorMessages['type']?></label>
						</div>
					<?php endif;?>
					
					<br /><br />
					<span>Complemento: </span>
					<input type="text" class="text" name="comple" id="comple" maxlength="65" <?php if(isset($this->errorMessages['comp'])):?>placeholder="<?php echo $this->errorMessages['comp']?>" <?php else :?>value="<?php echo $this->address['comp']?>" <?php endif;?>>
					<br /><br />
					<span>Referência: </span>
					<input value="<?php echo $this->client['ref']?>" type="text" class="text" name="ref" id="ref" maxlength="65" <?php if(isset($this->errorMessages['ref'])):?>placeholder="<?php echo $this->errorMessages['ref']?>"<?php endif;?>>
					<br /><br />
					<span>Cidade: </span>
					<input type="hidden" name="cityE" id="cityE" value="<?php echo $this->client['city']?>">
					<select name="city" id="citye">
						<option value="">Selecione</option>
						<?php foreach ($this->cities as $city):?>
							<option <?php if($this->client['city'] == utf8_encode($city['des_city'])):?>selected="selected"<?php endif;?> value="<?php echo  utf8_encode($city['des_city'])?>"><?php echo  utf8_encode($city['des_city'])?></option>
						<?php endforeach;?>
					</select>
					<br /><br />
					
					<span>Estado: </span>
					<select name="state" id="states">
						<option value="">Selecione</option>
						<?php foreach ($this->states as $state):?>
							<option <?php if($this->client['state'] == $state['des_state']):?>selected="selected"<?php endif;?> value="<?php echo $state['des_state']?>"><?php echo $state['des_state']?></option>
						<?php endforeach;?>
					</select>
					<br /><br />
					<input id="send-address" type="button" value="Editar" style="float: right; margin-right: 10px;">
					<br />
				</form>
			</div>
		</div>
	</div>
</div>